Micron Document
🎖️GitЯра🎖️

Node / meshtastic / Meshtastic-Android / files / core / data / src / commonMain / kotlin / org / meshtastic / core / data / repository / DeviceLinkRepositoryImpl.kt

Displaying Raw • Download

core/data/src/commonMain/kotlin/org/meshtastic/core/data/repository/DeviceLinkRepositoryImpl.kt 7631db63d0bdff28078438b095c0259306cb6617 (7631db63) Text, 5.21 KB

T8b949e/*
* Copyright (c) 2026 Meshtastic LLC
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
Tff7b72package T7ee787org.meshtastic.core.data.repository

Tff7b72import T7ee787co.touchlab.kermit.Logger
Tff7b72import T7ee787kotlinx.coroutines.flow.Flow
Tff7b72import T7ee787kotlinx.coroutines.flow.emitAll
Tff7b72import T7ee787kotlinx.coroutines.flow.flow
Tff7b72import T7ee787kotlinx.coroutines.flow.map
Tff7b72import T7ee787kotlinx.coroutines.sync.Mutex
Tff7b72import T7ee787kotlinx.coroutines.sync.withLock
Tff7b72import T7ee787org.koin.core.annotation.Single
Tff7b72import T7ee787org.meshtastic.core.common.util.safeCatching
Tff7b72import T7ee787org.meshtastic.core.data.datasource.DeviceHardwareLocalDataSource
Tff7b72import T7ee787org.meshtastic.core.data.datasource.DeviceLinkLocalDataSource
Tff7b72import T7ee787org.meshtastic.core.data.datasource.MshToLinksJsonDataSource
Tff7b72import T7ee787org.meshtastic.core.database.entity.asEntity
Tff7b72import T7ee787org.meshtastic.core.database.entity.asExternalModel
Tff7b72import T7ee787org.meshtastic.core.model.DeviceLink
Tff7b72import T7ee787org.meshtastic.core.model.MshToMarketplace
Tff7b72import T7ee787org.meshtastic.core.repository.DeviceLinkRepository

Tf0883e@Single
Tff7b72class T56d364DeviceLinkRepositoryImplTb4b4b4(
Tff7b72private Tff7b72val Te6edf3jsonDataSourceTb4b4b4: Te6edf3MshToLinksJsonDataSourceTb4b4b4,
Tff7b72private Tff7b72val Te6edf3localDataSourceTb4b4b4: Te6edf3DeviceLinkLocalDataSourceTb4b4b4,
Tff7b72private Tff7b72val Te6edf3deviceHardwareLocalDataSourceTb4b4b4: Te6edf3DeviceHardwareLocalDataSourceTb4b4b4,
Tb4b4b4) Tb4b4b4: Te6edf3DeviceLinkRepository Tb4b4b4{

T8b949e/** Guards the import so concurrent collectors don't run it more than once at a time. */
Tff7b72private Tff7b72val Te6edf3importMutex Tff7b72= Te6edf3MutexTb4b4b4(Tb4b4b4)

Tff7b72override Tff7b72suspend Tff7b72fun Td2a8ffensureImportedTb4b4b4(Tb4b4b4) Tb4b4b4{
Tff7b72if Tb4b4b4(Te6edf3localDataSourceTb4b4b4.Te6edf3countTb4b4b4(Tb4b4b4) Tff7b72> T79c0ff0Tb4b4b4) Tff7b72return
Te6edf3importMutexTb4b4b4.Te6edf3withLock Tb4b4b4{ Tff7b72if Tb4b4b4(Te6edf3localDataSourceTb4b4b4.Te6edf3countTb4b4b4(Tb4b4b4) Tff7b72=Tff7b72= T79c0ff0Tb4b4b4) Te6edf3doImportTb4b4b4(Tb4b4b4) Tb4b4b4}
Tb4b4b4}

Tff7b72override Tff7b72suspend Tff7b72fun Td2a8ffreconcileTb4b4b4(Tb4b4b4) Tb4b4b4{
Te6edf3importMutexTb4b4b4.Te6edf3withLock Tb4b4b4{ Te6edf3doImportTb4b4b4(Tb4b4b4) Tb4b4b4}
Tb4b4b4}

Tff7b72override Tff7b72suspend Tff7b72fun Td2a8ffgetLinksForTargetTb4b4b4(Te6edf3platformioTargetTb4b4b4: Tffa657StringTb4b4b4, Te6edf3regionCodeTb4b4b4: Tffa657StringTb4b4b4)Tb4b4b4: Te6edf3ListTff7b72<Te6edf3DeviceLinkTff7b72> Tb4b4b4{
Tff7b72if Tb4b4b4(Te6edf3platformioTargetTb4b4b4.Te6edf3isBlankTb4b4b4(Tb4b4b4)Tb4b4b4) Tff7b72return Te6edf3emptyListTb4b4b4(Tb4b4b4)
Te6edf3ensureImportedTb4b4b4(Tb4b4b4)
Tff7b72val Te6edf3links Tff7b72= Te6edf3localDataSourceTb4b4b4.Te6edf3getAllTb4b4b4(Tb4b4b4)Tb4b4b4.Te6edf3map Tb4b4b4{ Tffa657itTb4b4b4.Te6edf3asExternalModelTb4b4b4(Tb4b4b4) Tb4b4b4}
Tff7b72val Te6edf3marketplaceKeys Tff7b72= Te6edf3jsonDataSourceTb4b4b4.Te6edf3loadMarketplacesTb4b4b4(Tb4b4b4)Tb4b4b4.Te6edf3keys
Tff7b72val Te6edf3deviceTargets Tff7b72= Te6edf3deviceHardwareLocalDataSourceTb4b4b4.Te6edf3getAllTargetsTb4b4b4(Tb4b4b4)Tb4b4b4.Te6edf3toSetTb4b4b4(Tb4b4b4)
Tff7b72return Te6edf3DeviceLinkMatcherTb4b4b4.Te6edf3matchTb4b4b4(
Te6edf3links Tff7b72= Te6edf3linksTb4b4b4,
Te6edf3marketplaceKeys Tff7b72= Te6edf3marketplaceKeysTb4b4b4,
Te6edf3deviceTargets Tff7b72= Te6edf3deviceTargetsTb4b4b4,
Te6edf3target Tff7b72= Te6edf3platformioTargetTb4b4b4,
Te6edf3region Tff7b72= Te6edf3regionCodeTb4b4b4,
Tb4b4b4)
Tb4b4b4}

Tff7b72override Tff7b72fun Td2a8ffobserveAllLinksTb4b4b4(Tb4b4b4)Tb4b4b4: Te6edf3FlowTff7b72<Te6edf3ListTff7b72<Te6edf3DeviceLinkTff7b72>Tff7b72> Tff7b72= Te6edf3flow Tb4b4b4{
Te6edf3ensureImportedTb4b4b4(Tb4b4b4)
Te6edf3emitAllTb4b4b4(Te6edf3localDataSourceTb4b4b4.Te6edf3observeAllTb4b4b4(Tb4b4b4)Tb4b4b4.Te6edf3map Tb4b4b4{ Te6edf3entities Tff7b72-Tff7b72> Te6edf3entitiesTb4b4b4.Te6edf3map Tb4b4b4{ Tffa657itTb4b4b4.Te6edf3asExternalModelTb4b4b4(Tb4b4b4) Tb4b4b4} Tb4b4b4}Tb4b4b4)
Tb4b4b4}

T8b949e/** Loads bundled `urls.json`, classifies each short code, upserts, and prunes orphans. Mirrors Apple's import. */
Tff7b72private Tff7b72suspend Tff7b72fun Td2a8ffdoImportTb4b4b4(Tb4b4b4) Tb4b4b4{
Te6edf3safeCatching Tb4b4b4{
Tff7b72val Te6edf3routes Tff7b72= Te6edf3jsonDataSourceTb4b4b4.Te6edf3loadRoutesTb4b4b4(Tb4b4b4)
Tff7b72if Tb4b4b4(Te6edf3routesTb4b4b4.Te6edf3isEmptyTb4b4b4(Tb4b4b4)Tb4b4b4) Tb4b4b4{
Te6edf3LoggerTb4b4b4.Te6edf3w Tb4b4b4{ Ta5d6ff"Ta5d6ffDeviceLinkRepository: no routes in bundled urls.json; skipping importTa5d6ff" Tb4b4b4}
Tff7b72returnTf0883e@safeCatching
Tb4b4b4}
Tff7b72val Te6edf3marketplaces Tff7b72= Te6edf3jsonDataSourceTb4b4b4.Te6edf3loadMarketplacesTb4b4b4(Tb4b4b4)
Tff7b72val Te6edf3deviceTargets Tff7b72= Te6edf3deviceHardwareLocalDataSourceTb4b4b4.Te6edf3getAllTargetsTb4b4b4(Tb4b4b4)Tb4b4b4.Te6edf3toSetTb4b4b4(Tb4b4b4)

Tff7b72val Te6edf3links Tff7b72=
Te6edf3routesTb4b4b4.Te6edf3map Tb4b4b4{ Te6edf3route Tff7b72-Tff7b72>
Tff7b72val Te6edf3isVendor Tff7b72= Te6edf3routeTb4b4b4.Te6edf3shortCode Tff7b72in Te6edf3deviceTargets
Te6edf3DeviceLinkTb4b4b4(
Te6edf3shortCode Tff7b72= Te6edf3routeTb4b4b4.Te6edf3shortCodeTb4b4b4,
Te6edf3originalUrl Tff7b72= Te6edf3routeTb4b4b4.Te6edf3originalUrlTb4b4b4,
Te6edf3description Tff7b72= Te6edf3routeTb4b4b4.Te6edf3descriptionTb4b4b4,
Te6edf3isVendor Tff7b72= Te6edf3isVendorTb4b4b4,
Te6edf3regions Tff7b72= Tff7b72if Tb4b4b4(Te6edf3isVendorTb4b4b4) Tff7b72null Tff7b72else Te6edf3marketplaceRegionsTb4b4b4(Te6edf3routeTb4b4b4.Te6edf3shortCodeTb4b4b4, Te6edf3marketplacesTb4b4b4)Tb4b4b4,
Tb4b4b4)
Tb4b4b4}

Te6edf3localDataSourceTb4b4b4.Te6edf3upsertAllTb4b4b4(Te6edf3linksTb4b4b4.Te6edf3map Tb4b4b4{ Tffa657itTb4b4b4.Te6edf3asEntityTb4b4b4(Tb4b4b4) Tb4b4b4}Tb4b4b4)
Te6edf3localDataSourceTb4b4b4.Te6edf3deleteNotInTb4b4b4(Te6edf3linksTb4b4b4.Te6edf3map Tb4b4b4{ Tffa657itTb4b4b4.Te6edf3shortCode Tb4b4b4}Tb4b4b4)
Te6edf3LoggerTb4b4b4.Te6edf3i Tb4b4b4{ Ta5d6ff"Ta5d6ffDeviceLinkRepository: imported Tffd700${Te6edf3linksTb4b4b4.Te6edf3sizeTffd700}Ta5d6ff msh.to linksTa5d6ff" Tb4b4b4}
Tb4b4b4}
Tb4b4b4.Te6edf3onFailure Tb4b4b4{ Te6edf3LoggerTb4b4b4.Te6edf3wTb4b4b4(Tffa657itTb4b4b4) Tb4b4b4{ Ta5d6ff"Ta5d6ffDeviceLinkRepository: device links import failedTa5d6ff" Tb4b4b4} Tb4b4b4}
Tb4b4b4}

T8b949e/**
* Shipping regions for a marketplace short code, or null when it is not a marketplace link. Uses the same
* delimiter-aware classifier as the matcher/UI so a code's classification (vendor/variant vs marketplace) is
* consistent everywhere — independent of the `match` hint in `marketplaces.json`, which is unreliable in practice
* (e.g. AliExpress is declared `suffix` yet most codes use the `aliexpress-<target>` prefix form).
*/
Tff7b72private Tff7b72fun Td2a8ffmarketplaceRegionsTb4b4b4(Te6edf3codeTb4b4b4: Tffa657StringTb4b4b4, Te6edf3marketplacesTb4b4b4: Te6edf3MapTff7b72<Tffa657StringTb4b4b4, Te6edf3MshToMarketplaceTff7b72>Tb4b4b4)Tb4b4b4: Te6edf3ListTff7b72<Tffa657StringTff7b72>Tff7b72? Tff7b72=
Te6edf3DeviceLinkMatcherTb4b4b4.Te6edf3marketplaceKeyForTb4b4b4(Te6edf3codeTb4b4b4, Te6edf3marketplacesTb4b4b4.Te6edf3keysTb4b4b4)Tff7b72?.Te6edf3let Tb4b4b4{ Te6edf3marketplacesTb4b4b4.Te6edf3getValueTb4b4b4(Tffa657itTb4b4b4)Tb4b4b4.Te6edf3regions Tb4b4b4}
Tb4b4b4}

Served by rngit 1.5.0 - Generated in 0.08s